<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    width="60%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabIndex">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules" 
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="产品系列名称" prop="brandName" v-if="tabIndex==1">
        <a-input v-model="form.brandName" placeholder="文件名" />
      </a-form-model-item>
      <a-form-model-item label="英文文件名" v-else prop="brandNameEn">
        <a-input v-model="form.brandNameEn" placeholder="英文文件名" />
      </a-form-model-item>
      <!-- <a-form-model-item label="描述" prop="description" v-if="tabIndex==1">
        <a-input v-model="form.description" placeholder="描述" />
      </a-form-model-item>
      <a-form-model-item label="英文描述" v-else>
        <a-input v-model="form.descriptionEn" placeholder="英文描述" />
      </a-form-model-item> -->
      <a-form-model-item label="排序">
        <a-input v-model="form.sortNum" placeholder="排序" />
      </a-form-model-item>
      <a-form-model-item label="系列图片" required>
        <a-upload
          list-type="picture-card"
          :customRequest="handleUploadChange"
          :multiple="false"
          :file-list="fileList"
          :remove="hanldeImgRemove"
          accept="image/*"
        >
          <div class="custom-btn" v-if="fileList.length<1">
            <a-icon type="ant-cloud" style="font-size: 26px" />
            <div class="ant-upload-text">添加图片</div>
          </div>
        </a-upload>
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
  </a-drawer>
</template>

<script>
import { insertBrand,updateBrand } from '@/api/manage'
import { showMessage } from '@/utils/mixins'
import { uploadFile,deleteFile } from '@/api/common'
import { getFileName } from '@/utils/util'

export default {
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  mixins: [showMessage],
  data() {
    return {
      tabIndex: '1',
      visible: false,
      title: '',
      labelCol: {span: 3},
      wrapperCol: {span: 21},
      form: {
        brandName: undefined,
        brandNameEn: undefined,
        img: undefined,
        sortNum: 0
      },
      rules: {
        brandName: [{required: true, message: '产品系列名称不能为空', trigger: 'blur'}],
        brandNameEn: [{required: true, message: '产品系列名称英文不能为空',trigger: 'blur' }],
      },
      status: undefined,
      treeData: [],
      fileList: [],
      id: undefined
    }
  },
  methods: {
    // 删除图片
    hanldeImgRemove(file) {
      deleteFile({id: file.id}).then(res => {
        if(res.code == 0) {
          this.showMessage(res,() => {
            this.fileList = []
            this.form.img = undefined
          })
        }
      })
    },
    //自定义上传文件
    handleUploadChange(file) {
      const _this = this
      file = file.file
      const formdata = new FormData()
      formdata.append('file', file)
      uploadFile(formdata).then(res => {
        if(res.code == 0) {
          file.url = this.parent.originUrl + res.data.url
          file.uid = res.data._id
          file.id = res.data._id
          file.fileame = getFileName( res.data.url )
          _this.fileList.push(file)
          _this.form.img = res.data._id
        }else {
          _this.$message.error(res.message)
        }
      })
    },
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(!this.fileList.length) {
            this.$message.warning('产品系列图片不能为空！')
            return
          }
          if(this.status == 1) {
            insertBrand({
              ...this.form
            }).then(res => {
              this.showMessage(res,() => {
                this.onClose()
                this.parent.getList()
              })
            })  
          }else {
            updateBrand({
              id: this.id,
              ...this.form
            }).then(res => {
              this.showMessage(res,() => {
                this.onClose()
                this.parent.getList()
              })
            })  
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      this.id = undefined
      this.status = undefined
      this.visible = false
      this.fileList = []
      for(const key in this.form) {
        if(key == 'sortNum') {
          this.form[key] = 0
        } else {
          this.form[key] = undefined
        }
      }
    }
  }
}
</script>